from mutagen.mp3 import MP3
import supplyingant
from mutagen.id3 import ID3, APIC,TALB,TCON
from ant import Ant
import threading,Queue,time,os
class painter(threading.Thread,Ant):
        pile = Queue.Queue(5)
        def run(self):
                while True:
                        twig = painter.pile.get()
                        #print("painter")
                        Ant.leave("painter")
                        d=self.fetch(twig)
                        if(d==""):
                                painter.pile.put(twig)
                        Ant.comehome("painter")
                        painter.pile.task_done()
        def fetch(self,pt):

            if("g" in Ant.__commandstring__ and "a" not in Ant.__commandstring__):
                if('genre_tag' in pt):
                    if(pt['genre_tag']!=""):
                        pt['file_to_tag'].tags.add(TCON(encoding=3, text=pt['genre_tag']))
                        supplyingant.supplier.succeed(pt['artist_tag'],pt['genre_tag'])
        
            if("a" in Ant.__commandstring__):
                if('album_tag' in pt):
                    if(pt['album_tag']!="" and  " unter uns" not in pt['album_tag']):
                        print pt['album_tag'].encode('utf-8')
                        pt['file_to_tag'].tags.add(TALB(encoding=3, text=pt['album_tag']))
                if('album_art' in pt):
                    if(pt['album_art']!=""):
                        pt['file_to_tag'].tags.add(APIC(encoding=3,mime='image/jpeg',type=3,desc=u'Cover',data=pt['album_art']))
            try:
                    pt['file_to_tag'].save()
            except: raise
            del pt            
            return "success"
